//
//  CustomPageControl.m
//  dingding
//
//  Created by LHL on 14/12/18.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "CustomPageControl.h"

@implementation CustomPageControl{
    UIImage* activeImage;
    UIImage* inactiveImage;
}
-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        activeImage = [UIImage imageNamed:@"dot_select.png"];
        inactiveImage = [UIImage imageNamed:@"dot_normal.png"];
    }
    return self;
}

-(void)updateDots{
    for (int i = 0; i < self.subviews.count; i++) {
        UIImageView *dot = [self imageViewForSubview:[self.subviews objectAtIndex:i]];
        if (i == self.currentPage) {
            dot.image = activeImage;
        }else{
            dot.image = inactiveImage;
        }
    }
}

-(void)setCurrentPage:(NSInteger)currentPage{
    [super setCurrentPage:currentPage];
    [self updateDots];
}

- (UIImageView *) imageViewForSubview: (UIView *) view
{
    UIImageView * dot = nil;
    if ([view isKindOfClass: [UIView class]])
    {
        for (UIView* subview in view.subviews)
        {
            if ([subview isKindOfClass:[UIImageView class]])
            {
                dot = (UIImageView *)subview;
                break;
            }
        }
        if (dot == nil)
        {
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
            [view addSubview:dot];
        }
    }
    else
    {
        dot = (UIImageView *) view;
    }
    
    return dot;
}

@end
